import pandas as pd
import numpy as np
import datetime

train_data = pd.read_csv('weibo_train_data.txt', sep='\t')
train_data.columns = ['user_id', 'weibo_id', 'time', 'forward', 'comments', 'likes', 'tfidf', 'text']
train_data.head(5)


def process_max(data):
    # 训练集中出现的次数
    df_processnum = data['repost'].groupby(data['user_id']).agg('count')
    data['number_in_train'] = data['user_id'].apply(lambda x: df_processnum[str(x)])
    # 最大值
    df_processmax = data.groupby('user_id').agg({'repost': np.max, 'comments': np.max, 'likes': np.max})
    df_processmax.columns = ['forward_max', 'comment_max', 'like_max']
    df_processmax.reset_index(inplace=True)
    data = pd.merge(data, df_processmax, on=['user_id']).fillna(0)
    # 最小值
    df_processmin = data.groupby('user_id').agg({'repost': np.min, 'comments': np.min, 'likes': np.min})
    df_processmin.columns = ['forward_min', 'comment_min', 'like_min']
    df_processmin.reset_index(inplace=True)
    data = pd.merge(data, df_processmin, on=['user_id']).fillna(0)
    # 平均值
    df_processmean = data.groupby('user_id').agg({'repost': np.mean, 'comments': np.mean, 'likes': np.mean})
    df_processmean.columns = ['forward_mean', 'comment_mean', 'like_mean']
    df_processmean.reset_index(inplace=True)
    data = pd.merge(data, df_processmean, on=['user_id']).fillna(0)
    # 求某一用户发的微博互动大于平均值的概率
    daa = pd.DataFrame({'user_id': data['user_id'].value_counts()})
    daa.reset_index(inplace=True)
    daa.columns = ['user_id', 'count']
    # 统计大于平均值的发博次数
    forward_ave = np.mean(data['repost'])
    comment_ave = np.mean(data['comments'])
    like_ave = np.mean(data['likes'])
    data['forward_judge'] = data['repost'].apply(lambda x: 1 if x > forward_ave else 0)
    data['comment_judge'] = data['comments'].apply(lambda x: 1 if x > comment_ave else 0)
    data['like_judge'] = data['likes'].apply(lambda x: 1 if x > like_ave else 0)
    more_ave = data.groupby('user_id').agg({'forward_judge': np.sum, 'comment_judge': np.sum, 'like_judge': np.sum})
    more_ave.columns = ['forward_more_ave', 'comment_more_ave', 'like_more_ave']
    more_ave.reset_index(inplace=True)
    data.drop(['forward_judge', 'comment_judge', 'like_judge'], axis=1, inplace=True)
    daa = pd.merge(more_ave, daa, on=['user_id'])
    daa['forward_more_ave_pr'] = daa['forward_more_ave'] / daa['count']
    daa['comment_more_ave_pr'] = daa['comment_more_ave'] / daa['count']
    daa['like_more_ave_pr'] = daa['like_more_ave'] / daa['count']
    daa.drop(['forward_more_ave', 'comment_more_ave', 'like_more_ave', 'count'], axis=1, inplace=True)
    data = pd.merge(data, daa, on=['user_id']).fillna(0)
    data['max_f/l'] = pd.DataFrame(
        {'max_f/l': list(map(lambda x, y: x / y if y > 0 else 0, data['forward_max'], data['like_max']))})
    data['max_c/l'] = pd.DataFrame(
        {'max_c/l': list(map(lambda x, y: x / y if y > 0 else 0, data['comment_max'], data['like_max']))})
    data['min_f/l'] = pd.DataFrame(
        {'min_f/l': list(map(lambda x, y: x / y if y > 0 else 0, data['forward_min'], data['like_min']))})
    data['min_c/l'] = pd.DataFrame(
        {'min_c/l': list(map(lambda x, y: x / y if y > 0 else 0, data['comment_min'], data['like_min']))})
    data['mean_f/l'] = pd.DataFrame(
        {'mean_f/l': list(map(lambda x, y: x / y if y > 0 else 0, data['forward_mean'], data['like_mean']))})
    data['mean_c/l'] = pd.DataFrame(
        {'mean_c/l': list(map(lambda x, y: x / y if y > 0 else 0, data['comment_mean'], data['like_mean']))})
    return data


data = process_max(train_data)
data.head()


def process_time(data):
    data['time_date'] = data['time'].apply(lambda x: datetime.datetime.strptime(x, "%Y-%m-%d %H:%M:%S").date())
    # 星期几
    data['time_weekday'] = data['time_date'].apply(lambda x: x.weekday()) + 1
    data['time_weekend1'] = ((data['time_weekday'] == 6))
    data['time_weekend2'] = ((data['time_weekday'] == 7))
    # data['time_weekend']=(data.loc[1,'time_weekend1'])or(data.loc[1,'time_weekend2'])
    # 计算是否为周末
    data['time_weekend'] = pd.DataFrame(
        {'time_weekend': list(map(lambda x, y: 1 if x | y else 0, data['time_weekend1'], data['time_weekend2']))})
    # 发博时间段
    data['time_hour'] = data['time'].apply(lambda x: datetime.datetime.strptime(x, "%Y-%m-%d %H:%M:%S").hour)
    data.loc[data.apply(lambda data: (data['time_hour'] >= 1) and (data['time_hour'] < 7), axis=1), 'panduan'] = 1
    data.loc[data.apply(lambda data: (data['time_hour'] >= 7) and (data['time_hour'] < 12), axis=1), 'panduan'] = 2
    data.loc[data.apply(lambda data: (data['time_hour'] >= 12) and (data['time_hour'] < 18), axis=1), 'panduan'] = 3
    data.loc[data.apply(lambda data: (data['time_hour'] >= 18) and (data['time_hour'] < 24) or (data['time_hour'] == 0),
                        axis=1), 'panduan'] = 4
    data.drop(['time_date', 'time_weekend1', 'time_weekend2'], axis=1, inplace=True)
    return data


data = process_time(train_data)
data.head(5)
